import { store } from "../../../store/index";
import { editUserInfo, getTaskConfig, addAddr, getAddrDetail, editAddr } from "../../api/index";

Page({
  data: {
    formData: {
      addr: '',
      louceng: '',
      detail: '',
      phone: '',
      userName: '',
      isDefault: ''
    },
    taskConfig: [],
    id: '',
    showPicker: false,
    loucengShow: false,
    loucengList: []
  },

  onLoad(options) {
    // console.log(options, 'options');
    this.setData({
      id: options.id
    });
    wx.setNavigationBarTitle({
      title: options.id ? '编辑地址' : '添加地址'
    });
    if (options.id) {
      this.getAddrDetail( options.id );
    }
    this.getTaskConfig();
    this.getLoucengList();
  },

  handlePicker() {
    this.setData({
      showPicker: true
    });
  },
  loucengPicker() {
    this.setData({
      loucengShow: true
    });
  },
  onConfirm(event) {
    const {detail} = event.detail;
    console.log(detail, 'onConfirm');

    this.setData({
      showPicker: false,
      formData: {
        ...this.data.formData,
        addr:detail.value[0] + '-' + detail.value[1],
      }
    });
  },
  getLoucengList() {
   let loucengList = [];
    for (let i = 1; i <= 40; i++) {
      loucengList.push(`${i}楼`);
    }
    this.setData({
      loucengList
    });
  },
  loucengConfirm(event) {
    const {detail} = event.detail;
    // console.log(detail, 'loucengConfirm', this.data.formData);
    this.setData({
      loucengShow: false,
      formData: {
        ...this.data.formData,
        louceng: detail.value
      }
    });
  },
  loucengClose() {
    this.setData({
      loucengShow: false
    });
  },

  getAddrDetail( id ) {
    getAddrDetail({ id }).then(res => {
      console.log(res, 'ssssssss');
      this.setData({
        formData: {
          ...res.result,
          isDefault: res.result.isDefault == 1 ? true : false
        }
      });
    });
  },

  onClose() {
    this.setData({
      showPicker: false
    });
  },

  
  onChange(event) {
    console.log(event, 'eeeeeeeeeeee');
    const key = event.currentTarget.dataset.key;
    if (key === 'isDefault') {
      this.setData({
        [`formData.${key}`]: !this.data.formData.isDefault
      });
    } else {
      this.setData({
        [`formData.${key}`]: event.detail.value
      });
    }
  },

  getTaskConfig() {
    getTaskConfig().then(res => {
      console.log(res, 'ssssssss');
      let taskConfig = [
        {
          values: res.result,
          className: 'column1'
        },
        {
          values: this.getZhuzng(),
          className: 'column2'
        }
      ]
      this.setData({
        taskConfig
      });
    });
  },

  getZhuzng() {
    let zhuzng = [];
    for (let i = 1; i <= 40; i++) {
      zhuzng.push(`${i}幢`);
    }
    return zhuzng;
  },

  checkFormData() {
    if(!this.data.formData.addr) {
      wx.showToast({
        title: '请选择地址',
        icon: 'none'
      });
      return false;
    }
    if(!this.data.formData.louceng) {
      wx.showToast({
        title: '请选择楼层',
        icon: 'none'
      });
      return false;
    }
    if(!this.data.formData.detail) {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      });
      return false;
    }
    if(!this.data.formData.phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      });
      return false;
    }
    if(!this.data.formData.userName) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      });
      return false;
    }
    if (this.data.formData.phone.length !== 11 || !/^\d{11}$/.test(this.data.formData.phone)) {
      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none'
      });
      return false;
    }
    return true;
  },

  onSave() {
    if (!this.checkFormData()) {
      return;
    }
  
    if(this.data.id) {
      // 修改
      editAddr({
        ...this.data.formData,
        isDefault: this.data.formData.isDefault ? 1 : 0
      }).then(res => {
        console.log(res);
        wx.showToast({
          title: '修改成功',
          icon: 'success'
        });
        wx.navigateBack();
      });
    } else {
      // 添加
      addAddr({
        ...this.data.formData,
        isDefault: this.data.formData.isDefault ? 1 : 0
      }).then(res => {
        console.log(res);
        wx.showToast({
          title: '添加成功',
          icon: 'success'
        });
        wx.navigateBack();
      });
    }
  }
});
